package com.wonder.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.*;
import com.wonder.app.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "金蝶传单")
@RestController
@RequestMapping("/app/Sync")
public class SyncKingdeeController {


    @Autowired
    private PrdFeedService prdFeedService;

    @Autowired
    private PrdFeedDetailService prdFeedDetailService;

    @Autowired
    private PrdFeedDao prdFeedDao;

    @Autowired
    private PrdFeedDetailDao prdFeedDetailDao;

    @Autowired
    private SfcOptrptDao sfcOptrptDao;

    @Autowired
    private SfcOptrptDetailDao sfcOptrptDetailDao;

    @Autowired
    private PrdPickService prdPickService;
    @Autowired
    private PickDetailService pickDetailService;
    @Autowired
    private PrdPickDao prdPickDao;
    @Autowired
    private PickDetailDao pickDetailDao;

    @Autowired
    private PrdInstockDao prdInstockDao;

    @Autowired
    private PrdInstockDetailDao prdInstockDetailDao;

    @Autowired
    private SyncKingdeeOrderService syncKingdeeOrderService;

    @Autowired
    private PrdInstockService prdInstockService;

    /**
     * 补料单传金蝶领料
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "补料单传金蝶领料")
    @ResponseBody
    @PostMapping(value = "/sync_pick_by_feed")
    public Map<String, Object> getTray (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String billNo = VtuzxUtil.getString(map,"billNo");
        Long user_id = VtuzxUtil.getLong(map,"user_id");
        PrdFeed prdFeed = prdFeedDao.selectOne(new LambdaQueryWrapper<PrdFeed>()
                .eq(PrdFeed::getFBillNo, billNo));
        List<PrdFeedDetail> feedDetailList = prdFeedDetailDao.selectList(new LambdaQueryWrapper<PrdFeedDetail>()
                .eq(PrdFeedDetail::getFBILLNO, billNo));
        syncKingdeeOrderService.syncPrdPickByFeed(user_id,prdFeed,feedDetailList);
        return null;
//        return  trayService.getTray(map);
    }

    /**
     * 调拨
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "补料单传金蝶调拨")
    @ResponseBody
    @PostMapping(value = "/sync_pick_by_Allo")
    public Map<String, Object> getTrayAllocation (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String billNo = VtuzxUtil.getString(map,"billNo");
        Long user_id = VtuzxUtil.getLong(map,"user_id");
        PrdFeed prdFeed = prdFeedDao.selectOne(new LambdaQueryWrapper<PrdFeed>()
                .eq(PrdFeed::getFBillNo, billNo));
        List<PrdFeedDetail> feedDetailList = prdFeedDetailDao.selectList(new LambdaQueryWrapper<PrdFeedDetail>()
                .eq(PrdFeedDetail::getFBILLNO, billNo));
        syncKingdeeOrderService.syncAllocationByFeed(user_id,prdFeed,feedDetailList);
        return null;
//        return  trayService.getTray(map);
    }

    @ApiOperation(value = "领料单传金蝶领料")
    @ResponseBody
    @PostMapping(value = "/sync_pick_by_pick")
    public Map<String, Object> getTrayPick (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        List<String> billNoList = VtuzxUtil.getObject(map,"billNoList");
        for (String billNo : billNoList){

            Long user_id = VtuzxUtil.getLong(map,"user_id");
            PrdPick prdPick = prdPickDao.selectOne(new LambdaQueryWrapper<PrdPick>()
                    .eq(PrdPick::getFBILLNO, billNo));
            List<PickDetail> pickDetailList = pickDetailDao.selectList(new LambdaQueryWrapper<PickDetail>()
                    .eq(PickDetail::getFBILLNO, billNo));
            try {
                syncKingdeeOrderService.syncPrdPick(user_id,prdPick,pickDetailList);
            } catch (Exception e){
                System.out.println(e.getMessage());
            }
        }

        return null;
//        return  trayService.getTray(map);
    }


    @ApiOperation(value = "传工序汇报")
    @ResponseBody
    @PostMapping(value = "/sync_oprpt")
    public Map<String, Object> syncOprpt (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        Long user_id = VtuzxUtil.getLong(map,"user_id");
        String proplanno = VtuzxUtil.getString(map,"proplanno");
        String trayNo = VtuzxUtil.getString(map,"trayNo");
        syncKingdeeOrderService.syncPrdReportJiJia(user_id,proplanno,trayNo);
        return null;
//        return  trayService.getTray(map);
    }

    @ApiOperation(value = "传工序汇报铸造")
    @ResponseBody
    @PostMapping(value = "/sync_report_zhu_zao")
    public Map<String, Object> syncReportZhuZao (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        List<String> billNoList = VtuzxUtil.getObject(map,"billNoList");
        for (String billNo : billNoList){

            Long user_id = VtuzxUtil.getLong(map,"user_id");
            SfcOptrpt sfcOptrpt = sfcOptrptDao.selectOne(new LambdaQueryWrapper<SfcOptrpt>()
                    .eq(SfcOptrpt::getBillNo, billNo));
            List<SfcOptrptDetail> sfcOptrptDetailList = sfcOptrptDetailDao.selectList(new LambdaQueryWrapper<SfcOptrptDetail>()
                    .eq(SfcOptrptDetail::getBillNo, billNo));
            try {
                syncKingdeeOrderService.syncPrdReport(user_id,sfcOptrpt,sfcOptrptDetailList);
            } catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
        return null;
//        return  trayService.getTray(map);
    }


    @ApiOperation(value = "传入库")
    @ResponseBody
    @PostMapping(value = "/sync_prd_in_stock")
    public Map<String, Object> syncPrdInStock (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        Long user_id = VtuzxUtil.getLong(map,"user_id");
        List<String> billNoList = VtuzxUtil.getObject(map,"billNoList");
        for (String billNo : billNoList){
            PrdInstock prdInstock = prdInstockDao.selectOne(new LambdaQueryWrapper<PrdInstock>()
                    .eq(PrdInstock::getFBillNo, billNo));
            List<PrdInstockDetail> prdInstockDetailList = prdInstockDetailDao.selectList(new LambdaQueryWrapper<PrdInstockDetail>()
                    .eq(PrdInstockDetail::getFBILLNO, billNo));
            syncKingdeeOrderService.syncPrdInsStock(user_id,prdInstock,prdInstockDetailList);
        }

        return null;
//        return  trayService.getTray(map);
    }


    @ApiOperation(value = "领料单传金蝶调拨")
    @ResponseBody
    @PostMapping(value = "/sync_allocation_by_pick")
    public Map<String, Object> syncAllocationByPick (HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        List<String> billNoList = VtuzxUtil.getObject(map,"billNoList");
        for (String billNo : billNoList){

            Long user_id = VtuzxUtil.getLong(map,"user_id");
            PrdPick prdPick = prdPickDao.selectOne(new LambdaQueryWrapper<PrdPick>()
                    .eq(PrdPick::getFBILLNO, billNo));
            List<PickDetail> pickDetailList = pickDetailDao.selectList(new LambdaQueryWrapper<PickDetail>()
                    .eq(PickDetail::getFBILLNO, billNo));
            for (PickDetail pickDetail : pickDetailList){
                String trayno = pickDetail.getTRAYNO();
                PrdInstock prdInstock = prdInstockDao.selectOne(new LambdaQueryWrapper<PrdInstock>()
                        .eq(PrdInstock::getTRAYNO, trayno)
                        .eq(PrdInstock::getFWORKSHOPNO, "SX"));
                if (prdInstock.getFSTATE().equals("save")){
                    List<PrdInstockDetail> prdInstockDetailList = prdInstockDetailDao.selectList(new LambdaQueryWrapper<PrdInstockDetail>()
                            .eq(PrdInstockDetail::getFBILLNO, prdInstock.getFBillNo()));
                    prdInstockService.auditPrdInSM(user_id,"sunyf",prdInstock,prdInstockDetailList);
                }
            }
            try {
                syncKingdeeOrderService.syncAllocation(user_id,prdPick,pickDetailList);
                prdPickDao.updateById(prdPick);
            } catch (Exception e){
                throw new RuntimeException(prdPick.getFBILLNO() + e.getMessage());
            }
        }

        return null;
//        return  trayService.getTray(map);
    }
}
